from strategy.core.Stg import Stg
import talib.abstract as ta
from strategy.btStg.BaseStrategy import BaseStrategy


class DoubleMaStg(BaseStrategy):
    param = {
        'fastPeriod': 10,
        'slowPeriod': 30,
    }

    def initial(self):
        self.slowSma = ta.SMA(self.data, self.param['slowPeriod'])
        self.fastSma = ta.SMA(self.data, self.param['fastPeriod'])
        return

    def signal(self, index):
        # 自动跳过无法计算的节点
        if index < self.param['slowPeriod']:
            return
        pre = index - 1
        # 均线下穿,形成死叉,卖出
        if self.fastSma[pre] > self.slowSma[pre] and self.fastSma[index] <= self.slowSma[index]:  # 均线下穿
            return -1
        # 均线上穿
        if self.fastSma[pre] < self.slowSma[pre] and self.fastSma[index] >= self.slowSma[index]:  # 均线上穿
            return 1
